package com.example.rest_client_projet;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by richard on 18/03/14.
 */
public class GetEmployes  extends AsyncTask<Void, Void, String>  implements AdapterView.OnItemClickListener{
    Activity context;
    ProgressDialog mDialog;
    Employe [] Es;
    GetEmployes(Activity context, ProgressDialog mDialog) {
        super();
        this.context = context;
        this.mDialog = mDialog;

    }
    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
        InputStream in = entity.getContent();


        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n>0) {
            byte[] b = new byte[4096];
            n =  in.read(b);


            if (n>0) out.append(new String(b, 0, n));
        }


        return out.toString();
    }


    @Override


    protected String doInBackground(Void... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
       // HttpGet httpGet = new HttpGet("http://78.228.108.177/Symfony//web/app_dev.php/api/employes.json");
        HttpGet httpGet = new HttpGet("http://192.168.8.17/Symfony//web/app_dev.php/api/employes.json");
        String text = null;
        try {
            Log.e("REST #","GET");
            HttpResponse response = httpClient.execute(httpGet, localContext);


            HttpEntity entity = response.getEntity();


            text = getASCIIContentFromEntity(entity);




        } catch (Exception e) {
            Log.e("REST #","erreur");
            return e.getLocalizedMessage();
        }


        return text;
    }


    protected void onPostExecute(String results){
    if (results!=null) {


        JSONArray data = null;
        try {
            data = new JSONArray(results);

        //new ObjectMapper().readValue(results, MyObject.class);
            //Log.e("REST #",results);
             Es= Employe.arrayfromJson(results);


            List<Employe> Le  = Employe.listfromJson(results);

            Log.e("REST #","--> " + Es[1]+ "  \n " +Le.get(0));

            for (int i=0 ; i< data.length() ; i++ ) {
                JSONObject e = (JSONObject)data.get(i);
                Employe E = Employe.fromJson(e.toString());
             //  Log.e("REST #","--> " + E+"");
            }
            TextView texte = (TextView)context.findViewById(R.id.infotexte);
            texte.setText("OK");
            ListView listView = ( ListView )this.context.findViewById(R.id.listeemploye);
            ArrayAdapter<Employe> adapter = new ArrayAdapter<Employe>(this.context,  android.R.layout.simple_list_item_1,Es);
           listView.setAdapter(adapter);
            listView.setOnItemClickListener(this);
          //  ProgressBar mProgress = (ProgressBar) this.context.findViewById(R.id.progressBar);
           // mProgress.dism
            mDialog.dismiss();

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this.context, planningDay.class);
        intent.putExtra("id",Es[position].getId()+"");

        this.context.startActivity(intent);

    }
}



